package com.hyd.service;

import java.io.IOException;

import com.hyd.application.MyApplication;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.IBinder;

/**
 * 这是一个service，主要用于音乐的播放。MediaPlayer在prepare和release之前都需要先reset一下
 * 详见MediaPlayer的生命周期
 * 
 * @author llf
 * 
 */
public class MusicService extends Service implements OnCompletionListener {
	public static MediaPlayer player;
	public boolean isAudition = true;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		if (player == null) {
			// 创建一个要播放的多媒体
			player = new MediaPlayer();
			player.setOnCompletionListener(this);
		}
		super.onCreate();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		try {
			player.reset();
			player.setDataSource(MyApplication.musicList
					.get(MyApplication.which));// 设置数据源
			player.prepare();// 准备（同步）
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void pause() {
		player.pause();
	}

	public static void goon() {
		player.start();
	}

	public static void playNext() {
		if (player.isPlaying()) {
			player.stop();
		}
		MyApplication.which++;
		if (MyApplication.which >= MyApplication.musicList.size()) {
			MyApplication.which = 0;
		}
		try {
			player.reset();
			player.setDataSource(MyApplication.musicList
					.get(MyApplication.which));// 设置数据源
			player.prepare();// 准备（同步）
			player.setOnPreparedListener(new OnPreparedListener() {

				@Override
				public void onPrepared(MediaPlayer mp) {
					// 准备完毕，可以开始播放了
					player.start();
				}
			});
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void playPrior() {
		if (player.isPlaying()) {
			player.stop();
		}
		MyApplication.which--;
		if (MyApplication.which < 0) {
			MyApplication.which = MyApplication.musicList.size() - 1;
		}
		try {
			player.reset();
			player.setDataSource(MyApplication.musicList
					.get(MyApplication.which));// 设置数据源
			player.prepare();// 准备（同步）
			player.setOnPreparedListener(new OnPreparedListener() {

				@Override
				public void onPrepared(MediaPlayer mp) {
					// 准备完毕，可以开始播放了
					player.start();
				}
			});
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void audition(String name) {
		if (player.isPlaying()) {
			player.stop();
		}
		try {
			player.reset();
			player.setDataSource(name);// 设置数据源
			player.prepare();// 准备（同步）
			player.setOnPreparedListener(new OnPreparedListener() {

				@Override
				public void onPrepared(MediaPlayer mp) {
					// 准备完毕，可以开始播放了
					player.start();
				}
			});
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		playNext();
	}

	// Service关闭的时候，清空所有的资源
	@Override
	public void onDestroy() {
		if (player != null) {
			player.stop();
			player.reset();// 调用reset方法之后MediaPlayer处于空置状态
			player.release();// 调用release方法之后MediaPlayer处于结束状态
			player = null;
		}
		super.onDestroy();
	}
}
